import java.util.*;

public class Solution1636 {
    public int[] frequencySort(int[] nums) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0)+1);
        }
        List<Map.Entry<Integer,Integer>> list=new ArrayList<>(map.entrySet());
        list.sort((a,b)-> a.getValue().equals(b.getValue()) ?b.getKey()-a.getKey():a.getValue()-b.getValue());
        int[] ans=new int[nums.length];
        int count=0;
        for (Map.Entry<Integer, Integer> entry : list) {
            for (int integer = 0; integer < entry.getValue(); integer++) {
                ans[count++]=entry.getKey();
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1636().frequencySort(new int[]{1, 1, 2, 2, 2, 3})));
    }
}
